package com.youma.wt.mvpbaselib;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import java.util.ArrayList;
import java.util.List;

/**
 * 基类RecyclerView的adapter，RecyclerView.Adapter，默认holder为BaseHolder
 *
 * @author HaoXiong
 * @version 1.0
 */
public abstract class BaseRecylerViewAdapter<T, H> extends RecyclerView.Adapter<BaseHolder> {
    protected List<T> list;
    public BaseActivity mActivity;
    private boolean isClick;

    /**
     * @return 当前adapter的数据集合
     */
    public List<T> getList() {
        return list;
    }

    /**
     * @param layoutId 布局id
     * @param parent   父容器
     * @return 填充的View
     */
    public View getView(int layoutId, ViewGroup parent) {
        return LayoutInflater.from(mActivity).inflate(layoutId, parent, false);
//        return View.inflate(mActivity, layoutId,null);
    }

    /**
     * 构造方法
     *
     * @param mList    数据集合
     * @param activity activity
     */
    public BaseRecylerViewAdapter(List<T> mList, BaseActivity activity) {
        list = new ArrayList<T>();
        mActivity = activity;
        list.addAll(mList);
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return OnCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(final BaseHolder holder, int position) {
        if (isClick) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.OnItemClick(getParams(holder.getAdapterPosition()));
                }
            });
        }

        BaseHolder myHolder = (BaseHolder) holder;
        this.onBindViewHolderMy(myHolder, position);
    }

    /**
     * 刷新adapter
     * @param mlist 用于的数据集合
     */

    public void OnReferSh(List<T> mlist) {
        if (mlist != null) {
            list.clear();
            list.addAll(mlist);
            notifyDataSetChanged();
        }
    }

    /**
     * 往adapter中添加数据
     * @param mlist 新增的数据集合
     */
    public void OnLoadMore(List<T> mlist) {
        if (mlist != null && mlist.size() > 0) {
            list.addAll(mlist);
            notifyDataSetChanged();
        }
    }


    /**
     * item点击接口
     */
    public interface OnItemClickListener<H> {
        /**
         * RecylerView的item点击事件
         */
        void OnItemClick(H h);
    }

    public OnItemClickListener listener;

    /**
     * 设置item点击事件
     *
     * @param listener 监听器
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        isClick = true;
        this.listener = listener;
    }


    /**
     * @param position 当前点击的索引
     * @return 点击item后续所做的操作的字段
     */
    public abstract H getParams(int position);


    public abstract BaseHolder OnCreateViewHolder(ViewGroup parent, int viewType);


    public abstract void onBindViewHolderMy(BaseHolder myholder, int position);


}
